package Algorithm.Othes;

import java.util.Arrays;

//1005. K 次取反后最大化的数组和
//https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/
public class Leetcode1005 {
    class Solution {
        public int largestSumAfterKNegations(int[] nums, int k) {
            Arrays.sort(nums);
            int n=0;
            while(n<nums.length&&nums[n]<0&&k>0){
                nums[n] = -1 * nums[n];
                k--;
                n++;
            }

            Arrays.sort(nums);
            for(int i=0;i<k;i++){
                nums[0] = -1*nums[0];
            }

            int sum = 0;
            for(int i=0;i<nums.length;i++){
                sum += nums[i];
            }

            return sum;
        }
    }

}
